<template>

  <el-dialog
    v-model="dialogVisible"
    title="选择联系人"
    width="1000px"
    :before-close="handleClose"
  >

    <el-table
      height="350"
      border
      header-cell-class-name="table-header"
      :data="tableData"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column type="index" width="55" />
      <el-table-column header-align="center" align="center" label="联系人编号" prop="linkmanNumber" />
      <el-table-column header-align="center" align="center" label="联系人姓名" prop="linkmanName" />
      <el-table-column header-align="center" align="center" label="联系人电话" prop="tel" />

    </el-table>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="dialogConfirm">确定</el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script>

import {defineComponent, getCurrentInstance, inject, reactive, toRefs} from 'vue'

export default defineComponent({
  name: 'ContactDialog',
  emits:['confirm'],
  setup(props, { emit }) {
    const currentInstance = getCurrentInstance()
    const api = inject('api')

    var data = reactive({
      dialogVisible: false,
      selects: null,
      tableData: []
    })

    const openDialog = (order) => {
      let id = ''
      let type = ''
      if (order.orderType == 1) {
        if (!order.distributeId) {
          currentInstance.proxy.customMessage({
            type: 'error',
            message: '请选择配送商',
          })
          return
        } else {
          type = 'distribute'
          id = order.distributeId
        }
      } else if (order.orderType == 2) {
        if (!order.terminalId) {
          currentInstance.proxy.customMessage({
            type: 'error',
            message: '请选择终端',
          })
          return
        } else {
          type = 'terminal'
          id = order.terminalId
        }
      }else if (order.orderType == 3) {
        if (!order.agentId) {
          currentInstance.proxy.customMessage({
            type: 'error',
            message: '请选择代理商',
          })
          return
        } else {
          type = 'agent'
          id = order.agentId
        }
      }else if (order.orderType == 4) {
        if (!order.sellerId) {
          currentInstance.proxy.customMessage({
            type: 'error',
            message: '请选择商销客户',
          })
          return
        } else {
          type = 'seller'
          id = order.sellerId
        }
      }
      const queryParams = {
        relationId: Number(id),
        modelType: type,
        pageSize: 100,
        page: 1,
      }

      api.terminal.CONTACT_LIST(queryParams).then((res) => {
        if (res.code === 0) {
          data.tableData = res.data.list || []

          data.dialogVisible = true
        }
      })
    }

    const handleClose = () => {
      data.dialogVisible = false

      data.selects = null
    }

    const dialogConfirm = () => {
      if (!data.selects) {
        currentInstance.proxy.customMessage({
          type: 'error',
          message: '请选择联系人',
        })
        return
      }
      const str = { ...data.selects }

      emit('confirm', str)

      handleClose()
    }

    const handleSelectionChange = (val) => {
      data.selects = val
    }

    const handleCurrentChange = (val) => {
      data.selects = val
    }

    return {
      ...toRefs(data),

      openDialog,
      handleClose,
      dialogConfirm,
      handleSelectionChange,
      handleCurrentChange,

    }
  }
})
</script>

<style scoped>

</style>
